package com.lab303.powerproject.controller;

import com.lab303.powerproject.util.YieldPredictData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FileController {
    private static final Logger LOG = LoggerFactory.getLogger(YieldPredictData.class);
    @GetMapping("/index")
    public String index() {
        return "moduleMgmt";
    }

    @PostMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload1(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException, IOException {
        String OS = System.getProperty("os.name").toLowerCase();
        String path = "/home/power/uploadfile/input/";
        if(OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")>0){
            path = "/";
        }else if(OS.indexOf("windows")>=0){
            path = "c:\\";
        }
        LOG.info("系统路径={}",request.getSession().getServletContext().getRealPath(""));
        if(!new File(path).exists()){
            new File(path).mkdirs();
        }
        file.transferTo(new File(path + file.getOriginalFilename()));
        Map<String, Object> result = new HashMap<>();
        result.put("status", "true");
        result.put("data", null);
        return result;
    }
}
